package com.chinasoft.movie.controller;


import com.chinasoft.movie.chat.ChatDao;
import com.chinasoft.movie.service.IUserService;
import com.chinasoft.movie.util.ToolUtil;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.*;
@Controller
@RequestMapping("/")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ValueOperations redisValue;
    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request){
        Map<String,Object> paramMap=getParamMap(request);//得到参数
          StringBuffer stringBuffer = new StringBuffer();
        //已知的参数有pageIndex,pageSize和查询条件的参数
//        List<Map<String,Object>> list=null;
//        if(paramMap.get("pageIndex")!=null&&paramMap.get("pageSize")!=null){
//            int totalCount=userService.selectUserCount(paramMap);//根据条件的内容进行数据的个数的查询
//            int pageIndex=Integer.parseInt(paramMap.get("pageIndex").toString());
//            int pageSize=Integer.parseInt(paramMap.get("pageSize").toString());
//            int totalPage=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
//            int start=(pageIndex-1)*pageSize;//分页的起始索引号
//            paramMap.put("totalCount",totalCount);
//            paramMap.put("pageSize",pageSize);
//            paramMap.put("totalPage",totalPage);
//            paramMap.put("start",start);
//            list=userService.selectUserList(paramMap);
//        }else{
//            list=userService.selectUserList(paramMap);
//        }
//        paramMap.put("list",list);
        //***************************************************************************************
        //public Map<String,Object> getSelectTableList(int totalCount,Map<String,Object> paramMap,List<Map<String,Object>> list){
        //int totalCount=userService.selectUserCount(paramMap);
//        System.out.println("totalCount"+totalCount);
//        System.out.println("paramMap"+paramMap);
//        System.out.println("list"+userService.selectUserList(paramMap).size());
        //paramMap=getSelectTableList(totalCount,paramMap);
        getSelectTableList(userService.selectUserCount(paramMap),paramMap).put("list",userService.selectUserList(paramMap));
        return new ModelAndView("index",paramMap);
    }
    @RequestMapping(value = "/insertUser", method = RequestMethod.GET)
    public String register(){
        return "register";
    }

    @RequestMapping(value = "/insertUser", method = RequestMethod.POST)
    public String insertUser(HttpServletRequest request){
        int count=userService.insertUser(getParamMap(request));
        return "redirect:/index";
    }

    //deleteUser
    @RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
    public String deleteUser(HttpServletRequest request){
        //int count=userService.insertUser(getParamMap(request));
       // String ids=request.getParameter("ids");
//        StringBuffer buff=new StringBuffer(request.getParameter("ids"));
//        buff.delete(buff.length()-1,buff.length());
//        System.out.println(buff.toString());
//        List<Integer> list=new ArrayList<Integer>();
//        String s[]=buff.toString().split(",");
//        for (int i=0;i<s.length;i++){
//            list.add(Integer.parseInt(s[i]));
//        }
        int count=userService.deleteUserByIds(getParamIdsList(request));
        return "redirect:/index";
    }

    @RequestMapping(value = "/ajax", method = RequestMethod.GET)
    public String ajax(){

        return "ajax";
    }


    @RequestMapping(value = "/checkAccount", method = RequestMethod.GET)
    public  @ResponseBody String checkAccount(HttpServletRequest request){
        List<String> list=new ArrayList<String>();
        list.add("admin1");
        list.add("admin2");
        String userAccount= request.getParameter("userAccount");
        String result="0";
        for (int i=0;i<list.size();i++){
            if(list.get(i).equals(userAccount)){
                result="1";
                break;
            }
        }
        return result;
    }

    @RequestMapping(value = "/checkAccount1", method = RequestMethod.GET)
    public  @ResponseBody String checkAccount1(HttpServletRequest request){
        StringBuffer buff=new StringBuffer();
        List<String> list=new ArrayList<String>();
        list.add("apple");
        list.add("banana");
        list.add("add");
        list.add("backetball");
        list.add("check");
        list.add("application");
        list.add("apply");
        String userAccount= request.getParameter("userAccount");
        for (int i=0;i<list.size();i++){
            if(userAccount.equals(list.get(i).substring(0,userAccount.length()))){
                buff.append(list.get(i)+",");
            }
        }
        if(buff.toString().length()!=0) {
            buff.delete(buff.length() - 1, buff.length());
        }
        return buff.toString();
    }



    @RequestMapping(value = "/checkAccount2", method = RequestMethod.GET)
    public  @ResponseBody List<Map<String,Object>> checkAccount2(HttpServletRequest request){
//        List<String> list=new ArrayList<String>();
//        list.add("apple");
//        list.add("banana");
//        list.add("add");
//        list.add("backetball");
//        list.add("check");
//        list.add("application");
//        list.add("apply");
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        Map<String,Object> map1=new HashMap<String,Object>();
        map1.put("name","admin1");
        map1.put("age",23);
        map1.put("sex","男");
        Map<String,Object> map2=new HashMap<String,Object>();
        map2.put("name","admin2");
        map2.put("age",24);
        map2.put("sex","女");
        list.add(map1);
        list.add(map2);
        return list;
    }

    @RequestMapping(value = "/chat", method = RequestMethod.GET)
    public  String  chat(HttpServletRequest request){
            if(request.getSession().getAttribute("nick")==null){
                return "redirect:/login";
            }
            return "chat";
    }

//    //地址栏输入login访问该方法进入登录页面
//    @RequestMapping(value = "/login", method = RequestMethod.GET)
//    public  String  login(HttpServletRequest request){
//        return "login";
//    }
//
//    @RequestMapping(value = "/checkNick", method = RequestMethod.GET)
//    public  @ResponseBody String  checkNick(HttpServletRequest request){
//        return ChatDao.checkNickIsHave(request);
//    }
//
//    @RequestMapping(value = "/login", method = RequestMethod.POST)
//    public  String  login1(HttpServletRequest request){
//        request.getSession().setAttribute("nick",request.getParameter("nick"));
//        ChatDao.nickList.add(request.getParameter("nick"));
//        return "redirect:/chat";
//    }
//
//    //loadNickList
//    @RequestMapping(value = "/loadNickList", method = RequestMethod.GET)
//    public  @ResponseBody List<String> loadNickList(HttpServletRequest request) {
//      return ChatDao.nickList;
//    }
//
//    @RequestMapping(value = "/loadInfoList", method = RequestMethod.GET)
//    public  @ResponseBody List<String> loadInfoList(HttpServletRequest request) {
//        return ChatDao.infoList;
//    }
//
//    //sendMessage
//    @RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
//    public  @ResponseBody String sendMessage(HttpServletRequest request) {
//        //如果没有接受到这个值那么发送就是失败的
//        if(request.getParameter("message")==null||request.getParameter("message").toString().equals("")){
//            return "0";
//        }else{//反之那么这个值我就必须要加入到信息集合里面去了
//            String newMessage=ChatDao.getNewMessage(request);
//            //System.out.println("newMessage"+newMessage);
//            ChatDao.infoList.add(newMessage);
//            return "1";
//        }
//
//    }

    //上传跳转到页面
    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public  String  upload(HttpServletRequest request){
        System.out.println("啊我进入到上传的方法里面来了");
        return "upload";
    }
//    //执行上传的代码
//    @RequestMapping(value = "/upload", method = RequestMethod.POST)
//    public  ModelAndView  upload1(@RequestParam("attr") List<MultipartFile> file, HttpServletRequest request){
//        // SELECT #{item.VIDEO_TITLE}, #{item.VIDEO_UPLOAD_TIME},#{item.VIDEO_IMG_URL},#{item.VIDEO_MOVIE_URL}  FROM DUAL
//        Map<String,Object> map=getParamMap(request);
//        List<String> list=upload(file,request);
//        StringBuffer buff=new StringBuffer(map.get("str").toString());
//        buff.delete(buff.length()-1,buff.length());//剪掉小,最后的一个,
//        String s[]=buff.toString().split(",");
//        for(int i=0;i<s.length;i++){//循环两次插入，第一次的insert into 要拿到List的前面两个值索引号为0和1的
//            String VIDEO_TITLE=map.get("VIDEO_TITLE"+s[i]).toString();//循环拿到视频标题的值
//            Date VIDEO_UPLOAD_TIME= new Timestamp(System.currentTimeMillis());//视频的上传的时间
//            String VIDEO_IMG_URL=list.get(i*2);
//            String VIDEO_MOVIE_URL=list.get(i*2+1);
//            Map<String,Object> mapData=new HashMap<String,Object>();
//            mapData.put("VIDEO_TITLE",VIDEO_TITLE);
//            mapData.put("VIDEO_UPLOAD_TIME",VIDEO_UPLOAD_TIME);
//            mapData.put("VIDEO_IMG_URL",VIDEO_IMG_URL);
//            mapData.put("VIDEO_MOVIE_URL",VIDEO_MOVIE_URL);
//            userService.insertVideo(mapData);
//
//       }
//        return new ModelAndView("photo",map);
//    }

    //执行上传的代码
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public  String  upload1(@RequestParam("attr") List<MultipartFile> file, HttpServletRequest request){
        // SELECT #{item.VIDEO_TITLE}, #{item.VIDEO_UPLOAD_TIME},#{item.VIDEO_IMG_URL},#{item.VIDEO_MOVIE_URL}  FROM DUAL
        Map<String,Object> map=getParamMap(request);
//        System.out.println("map"+map);
//        System.out.println("map"+map.get("str"));
        Object[] obj=upload(file,request);
        List<String> list=(List<String>)obj[0];//集合文件的地址
        List<Long> dataList=(List<Long>)obj[1];//集合文件的大小
        StringBuffer buff=new StringBuffer(map.get("str").toString());
        //System.out.println("buff1"+buff);
        buff.delete(buff.length()-1,buff.length());//剪掉小,最后的一个,
        //System.out.println("buff2"+buff);
        String s[]=buff.toString().split(",");
        List<Map<String,Object>> datalist=new ArrayList<Map<String,Object>>();
        for(int i=0;i<s.length;i++){//循环两次插入，第一次的insert into 要拿到List的前面两个值索引号为0和1的
            String VIDEO_TITLE=map.get("VIDEO_TITLE"+s[i]).toString();//循环拿到视频标题的值
            Date VIDEO_UPLOAD_TIME= new Timestamp(System.currentTimeMillis());//视频的上传的时间
            String VIDEO_IMG_URL=list.get(i*2);
            String VIDEO_MOVIE_URL=list.get(i*2+1);
            long VIDEO_SIZE=dataList.get(i*2+1);
            Map<String,Object> mapData=new HashMap<String,Object>();
            mapData.put("VIDEO_TITLE",VIDEO_TITLE);
            mapData.put("VIDEO_UPLOAD_TIME",VIDEO_UPLOAD_TIME);
            mapData.put("VIDEO_IMG_URL",VIDEO_IMG_URL);
            mapData.put("VIDEO_MOVIE_URL",VIDEO_MOVIE_URL);
            mapData.put("VIDEO_SIZE",VIDEO_SIZE);
            datalist.add(mapData);
        }
        int count=userService.insertVideo1(datalist);
        System.out.println("count"+count);
        return "redirect:/selectVideoList";
    }

//    public static void main(String[] args) {
//        for(int i=0;i<100;i++){
//            System.out.println(UUID.randomUUID().toString());
//        }
//    }

    @RequestMapping(value = "/movie", method = RequestMethod.GET)
    public String movie(){
        return "movie";
    }

    //selectVideoList
    @RequestMapping(value = "/selectVideoList", method = RequestMethod.GET)
    public  ModelAndView  selectVideoList(HttpServletRequest request){
        List<Map<String,Object>> list=userService.selectVideoList();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("list",list);
        map.put("space", ToolUtil.spaceInfo());
        map.put("recommendVideo",ToolUtil.videoMap);
        System.out.println("recommendVideo"+map);
        return new ModelAndView("videoList",map);
    }

    @RequestMapping(value = "/watch_video", method = RequestMethod.GET)
    public  ModelAndView  watch_video(HttpServletRequest request){
        Map<String,Object> map=getParamMap(request);
        map.put("video",userService.selectVideoById(map));
        System.out.println("map"+map);
        return new ModelAndView("watch_video",map);
    }

    //index1登录页面 index2success<a> index3<主页>


    //  @Autowired
    //    private RedisTemplate<String, Object> template;

    //跳转到登录页面
    @RequestMapping(value = "/index1", method = RequestMethod.GET)
    public  String  index1(HttpServletRequest request){
      //跳转到登录页面
        return "index1";
    }
//处理登录的页面
    @RequestMapping(value = "/index2", method = RequestMethod.POST)
    public  String  index2(HttpServletRequest request){
        //System.out.println(getParamMap(request));
        return userService.selectEmployeeLogin(getParamMap(request),request,redisValue);
    }

    //看是否能成功跳转到index3页面
    @RequestMapping(value = "/index3", method = RequestMethod.GET)
    public  String  index3(HttpServletRequest request){
        HttpSession session=request.getSession();
        List<String> list=(List<String>)session.getAttribute("account");
        String employeeAccount=list.get(0).toString();//登录大的员工的账号//admin1
        String sessionId=list.get(1).toString();//sessionId//ERT456YURRR
        String redisSessinId=redisValue.get(employeeAccount).toString();
        if(!sessionId.equals(redisSessinId)){
            session.setAttribute("message","你的账号被登录了你被迫下线!");
            return "redirect:/index1";
        }else{
            return "index3";
        }


    }

}
